Over the last couple of years American Football gained a lot of popularity in Europe especially Germany. Different than soccer, American football is a play based game. After each play the game gets interupted for the offense and defense to set up their team once again. The goal is to achieve a touchdown on the opponents endzone. To achieve that the offense works its way across the field play by play. With each play the offense tries to gain as many yards as possible while the defense tries to prevent this. There are 2 main kind of plays to gain yards: rushing and passing. For a rush plays the offensive team tries to gain yards by caring the ball. For pass plays the quarterback of the offensive team will throw to ball to another player. The key difference is the that for rushing plays the ball will not be airborn but handed over.
We want to find out if there is a way to predict how many yards a team will gain in a play based on variables that we know before the play starts.
Other people have already researched similiar questions but i.e. focused on predicting the rushing yards. We focus on yards generally gained no matter if rushed or passed, as this can not be determined before the play starts. Interesting links to check out for similiar analysis:
http://cs229.stanford.edu/proj2019aut/data/assignment_308832_raw/26588266.pdf
https://rpubs.com/woutcault/Final606_Multiple_Regression
https://medium.com/@matthewdmeans/predicting-yards-gained-in-the-nfl-3a0eea7a54a3
For NFL matches there is a comprehensive data asset available that includes play-by-play data, we will focus on the data gathered so far (until 26th December 2022) in the 2022 NFL season. It tracks a multitude of variables for each play. For our analysis we will focus on the variables that are known before the play starts as explanatory variables and the yards gained as the response variable. The data was scraped from the NFL Next Gen Stats website with the NFL verse package.
Data Source https://github.com/nflverse/nflverse-data/releases/tag/pbp
Data Description https://mrcaseb.github.io/pages_dummy/reference/fast_scraper.html
Our response variable will be "yards-gained" this variable describes how many yards a team gained (or lost) in a play excluding yards gained via fumble recoveries and laterals. Our possible explanatory variables can be found in the data dictionary below.
| Name | Description | Role | Type | Format | |
|---|---|---|---|---|---|
| 0 | yards_gained | Yards gained/lost in the play excluding yards ... | Response | numeric | float64 |
| 1 | quarter_seconds_remaining | Seconds on the clock until the quarter ends | Predictor | numeric | int64 |
| 2 | qtr | Current quater 1-4 and 5 for overtime | Predictor | numeric | int64 |
| 3 | down | Current down | Predictor | numeric | float64 |
| 4 | yardline_100 | Distance to the opponents endzone in yards | Predictor | numeric | float64 |
| 5 | ydstogo | Yards to the next first down | Predictor | numeric | int64 |
| 6 | score_differential | Score difference between offense team and the ... | Predictor | numeric | float64 |
| 7 | posteam | Offense Team | Predictor | nominal | object |
| 8 | defteam | Defense Team | Predictor | nominal | object |
| 9 | home_team | Home Team | Predictor | nominal | object |
| 10 | away_team | Guest/Away Team | Predictor | nominal | object |
We import the complete data set which also includes variables which can only be gathered after the play i.e. if a rush or a pass play was tried.
As we want to find out if there is a way to predict how many yards a team will gain based on variables that we know before the play starts, we reduce the variables to the following small list which represents the variables, that are already known before a play. We also drop all plays during which one of the variables is not available.
<class 'pandas.core.frame.DataFrame'> Int64Index: 35058 entries, 2 to 42322 Data columns (total 11 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 yards_gained 35058 non-null float64 1 home_team 35058 non-null object 2 away_team 35058 non-null object 3 quarter_seconds_remaining 35058 non-null int64 4 qtr 35058 non-null int64 5 down 35058 non-null float64 6 yardline_100 35058 non-null float64 7 ydstogo 35058 non-null int64 8 posteam 35058 non-null object 9 defteam 35058 non-null object 10 score_differential 35058 non-null float64 dtypes: float64(4), int64(3), object(4) memory usage: 3.2+ MB
We differentiate between categorial variables and numerical variables.
Next we split our data into explanatory variables and the response variables.
For the categorial variables we need to create dummy columns to fit them into our regression models.
We do a train/test split with 70/30 % of the data.
yards_gained 4.668435 quarter_seconds_remaining 424.119516 qtr 2.574391 down 1.997005 yardline_100 51.043585 ydstogo 8.464231 score_differential -1.414114 dtype: float64
| count | mean | std | min | 25% | 50% | 75% | max | |
|---|---|---|---|---|---|---|---|---|
| yards_gained | 35058.0 | 4.668435 | 8.131966 | -26.0 | 0.0 | 2.0 | 7.0 | 98.0 |
| quarter_seconds_remaining | 35058.0 | 424.119516 | 272.601532 | 0.0 | 174.0 | 413.0 | 659.0 | 900.0 |
| qtr | 35058.0 | 2.574391 | 1.135944 | 1.0 | 2.0 | 3.0 | 4.0 | 5.0 |
| down | 35058.0 | 1.997005 | 1.003640 | 1.0 | 1.0 | 2.0 | 3.0 | 4.0 |
| yardline_100 | 35058.0 | 51.043585 | 24.274817 | 1.0 | 32.0 | 55.0 | 71.0 | 99.0 |
| ydstogo | 35058.0 | 8.464231 | 4.081100 | 1.0 | 6.0 | 10.0 | 10.0 | 38.0 |
| score_differential | 35058.0 | -1.414114 | 9.296843 | -37.0 | -7.0 | 0.0 | 3.0 | 37.0 |
we have 35058 observations (individual plays)
We can see that many plays result in zero yards gained. This is due to plays being aborted i.e. by incomplete passes or fouls.
After filtering out the plays that result zero yards gained, we get a distribution, that looks like a positive skewed normal distribution, with a mean = 4.6.
Probability that a team gains positive yards with a play: 57.94
Probability that a team gains positive yards with a play is 57.94 %
Now we want to understand the relationship between our numerical variables.
Some relationships in the data can already be seen, most of them are game roles and restriction related i.e.
With a correlation matrix we want to test which of our variables correlate to each other by what amount. For that we chose the Spearman Method over the Pearson and Kendall Method. It does not really on normality of the data as the Pearson method does. And tests for correlation not dependence as the Kendall Method does
| quarter_seconds_remaining | qtr | down | yardline_100 | ydstogo | score_differential | yards_gained | |
|---|---|---|---|---|---|---|---|
| quarter_seconds_remaining | 1.000000 | -0.040000 | -0.030000 | 0.080000 | -0.000000 | 0.010000 | 0.010000 |
| qtr | -0.040000 | 1.000000 | 0.020000 | -0.040000 | 0.000000 | -0.030000 | -0.020000 |
| down | -0.030000 | 0.020000 | 1.000000 | -0.080000 | -0.460000 | -0.010000 | 0.050000 |
| yardline_100 | 0.080000 | -0.040000 | -0.080000 | 1.000000 | 0.220000 | -0.010000 | 0.090000 |
| ydstogo | -0.000000 | 0.000000 | -0.460000 | 0.220000 | 1.000000 | -0.000000 | 0.090000 |
| score_differential | 0.010000 | -0.030000 | -0.010000 | -0.010000 | -0.000000 | 1.000000 | -0.070000 |
| yards_gained | 0.010000 | -0.020000 | 0.050000 | 0.090000 | 0.090000 | -0.070000 | 1.000000 |
We can see a that most of the variables do not strongly correlate with each other.
ydstogo and down are quite strongly correlated as expected, because with each down the offensive team should come closer to the yardline it needs to reach.
ydstogo and yardline_100 also have some correlation which can be explained by the underlying rules of the game. The offensive team can not have more yards to go than they have until they enter the defenses endzone, the distance to which is represented by the yardline_100 variable. Therefor it's always ydstogo <= yardline_100
yards_gained and down also seem to have a correlation which can be explained by
In this section we will look at 3 different regression models (Linear regression, K-nearest neighbour, & Lasso-regression) as well as Model 4 were we just always predict the mean value as a sanity check.
| quarter_seconds_remaining | qtr | down | yardline_100 | ydstogo | score_differential | posteam_ari | posteam_atl | posteam_bal | posteam_buf | ... | away_team_no | away_team_nyg | away_team_nyj | away_team_phi | away_team_pit | away_team_sea | away_team_sf | away_team_tb | away_team_ten | away_team_was | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2 | 896 | 1 | 1.0 | 78.0 | 10 | 0.0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 3 | 869 | 1 | 1.0 | 59.0 | 10 | 0.0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 4 | 865 | 1 | 2.0 | 59.0 | 10 | 0.0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 5 | 841 | 1 | 3.0 | 54.0 | 5 | 0.0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 6 | 833 | 1 | 4.0 | 64.0 | 15 | 0.0 | 0 | 0 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
5 rows × 134 columns
Because we have a lot a variables, we would like to choose the 10 most relevant variables for the model to make it more sensitive in regards to these important variables. Doing so, we use Forward selection:
LinearRegression()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
LinearRegression()
After the model training we can check the 10 most important variables and their coefficient.
| Name | Coefficient | |
|---|---|---|
| 0 | Intercept | 4.661 |
| 1 | down | -0.896 |
| 2 | yardline_100 | 0.031 |
| 3 | score_differential | -0.028 |
| 4 | posteam_buf | 0.945 |
| 5 | posteam_kc | 1.244 |
| 6 | posteam_mia | 1.070 |
| 7 | posteam_phi | 0.914 |
| 8 | posteam_sf | 0.890 |
| 9 | home_team_den | -1.026 |
| 10 | home_team_det | 0.889 |
63.43250919198005
7.9644528495044815
5.307034643163722
'../models/linear_reg_model.sav_2023-01-16-21_38_42.sav'
KNeighborsRegressor(n_neighbors=2)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
KNeighborsRegressor(n_neighbors=2)
86.06705172085948
9.277232977610268
6.069832667807568
'../models/KN_Model.sav_2023-01-16-21_38_43.sav'
LassoCV(cv=5, max_iter=10000, random_state=0)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
LassoCV(cv=5, max_iter=10000, random_state=0)
0.0009685368136113017
Lasso(alpha=0.0009685368136113017)In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
Lasso(alpha=0.0009685368136113017)
R squared training set 2.55 R squared test set 2.28
63.43151846072908
7.964390652192362
5.307091618975074
'../models/lasso_reg_model.sav_2023-01-16-21_38_47.sav'
array([4.65334148, 4.65334148, 4.65334148, ..., 4.65334148, 4.65334148,
4.65334148])
64.91328211074314
8.056877938180715
5.451782561442774
Out of the 3 Models we evaluated only Linear regression and Lasso regression were able to beat our naive Model where we always predicted the mean value of the training set slightly.
Both Linear Regression and Lasso Regression performed essentially the same and only slightly better than the Dummy Model.
It seems that with the given variables we were not able to create a model which can predict yards gained in a football game reliable. This reflects the unpredictable nature of the game, which makes it enjoyable by millions of fans.
| Model | MSE | RMSE | MAE | |
|---|---|---|---|---|
| 0 | Linear Regression | 63.432509 | 7.964453 | 5.307035 |
| 1 | K-nearest neighbour | 86.067052 | 9.277233 | 6.069833 |
| 2 | Lasso Regression | 63.431518 | 7.964391 | 5.307092 |
| 3 | Mean Value Model | 64.913282 | 8.056878 | 5.451783 |